package com.qianhui.dao;

import com.qianhui.common.IResult;
import com.qianhui.pojo.GoodsCommission;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * A data access object (DAO) providing persistence and search support for
 * GoodsCommission entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 *
 * @author MyEclipse Persistence Tools
 * @see .GoodsCommission
 */
public class GoodsCommissionDAO extends BaseHibernateDAO
{
    private static final Logger log = LoggerFactory.getLogger(GoodsCommissionDAO.class);
    // property constants
    public static final String GOODS_ID = "goodsId";
    public static final String BUYER_ID = "buyerId";
    public static final String GOODS_COMMISSION = "com.qianhui.pojo.GoodsCommission";

    public GoodsCommission save(GoodsCommission transientInstance)
    {
        Transaction tx = null;
        try
        {
            tx = getSession().beginTransaction();
            getSession().saveOrUpdate(transientInstance);
            tx.commit();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
        return transientInstance;
    }

    public List<GoodsCommission> saveList(List<GoodsCommission> list)
    {
        Transaction tx = null;
        try
        {
            tx = getSession().beginTransaction();
            for (GoodsCommission temp : list)
            {
                getSession().saveOrUpdate(temp);
            }
            tx.commit();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
        return list;
    }

    public int delete(GoodsCommission persistentInstance)
    {
        Transaction tx = null;
        try
        {
            tx = getSession().beginTransaction();
            getSession().delete(persistentInstance);
            tx.commit();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return IResult.DELETE_EXCEPTION;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
        return IResult.DELETE_SUCCESS;
    }

    public int batch_delete(List<GoodsCommission> list)
    {
        Transaction tx = null;
        try
        {
            tx = getSession().beginTransaction();
            for (GoodsCommission l : list)
            {
                getSession().delete(l);
            }
            tx.commit();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return IResult.DELETE_EXCEPTION;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
        return IResult.DELETE_SUCCESS;
    }

    public GoodsCommission findById(java.lang.String id)
    {
        log.debug("getting GoodsCommission instance with id: " + id);
        try
        {
            GoodsCommission instance = (GoodsCommission) getSession().get("com.qianhui.pojo.GoodsCommission", id);
            return instance;
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }


    public List findByIdList(List<String> idList)
    {
        try
        {
            String queryString = "from GoodsCommission as model where model.goodsCommissionId in (:idList)";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameterList("idList", idList);
            return queryObject.list();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }


    public List findByProperty(String propertyName, Object value)
    {
        log.debug("finding GoodsCommission instance with property: " + propertyName + ", value: " + value);
        try
        {
            String queryString = "from GoodsCommission as model where model." + propertyName + "= ?";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameter(0, value);
            return queryObject.list();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List findByGoodsId(Object goodsId)
    {
        return findByProperty(GOODS_ID, goodsId);
    }

    public List findByBuyerId(Object buyerId)
    {
        return findByProperty(BUYER_ID, buyerId);
    }

    public List findByGoodsCommission(Object goodsCommission)
    {
        return findByProperty(GOODS_COMMISSION, goodsCommission);
    }

    public GoodsCommission findByGoodsIdAndBuyerId(String goodsId, String buyerId)
    {
        try
        {
            String sql = "from GoodsCommission gc where gc.goodsId=:goodsId and gc.buyerId=:buyerId";
            Query query = getSession().createQuery(sql);
            query.setParameter("goodsId", goodsId);
            query.setParameter("buyerId", buyerId);
            List tempList = query.list();
            if (tempList == null)
            {
                return null;
            }
            else
            {
                if (tempList.size() > 0)
                {
                    return (GoodsCommission) tempList.get(0);
                }
                else
                {
                    return null;
                }
            }
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List getListByBuyerIdAndIndexAndPageSize(String buyerId, int index, int pageSize)
    {
        try
        {
            String sql = "from GoodsCommission gc where gc.buyerId=:buyerId order by gc.buyerId asc,gc.goodsId asc, gc.commissionUpdateTime desc";
            Query query = getSession().createQuery(sql);
            query.setParameter("buyerId", buyerId);
            query.setFirstResult((index - 1) * pageSize);
            query.setMaxResults(pageSize);
            List tempList = query.list();
            if (tempList == null)
            {
                return null;
            }
            else
            {
                if (tempList.size() > 0)
                {
                    return tempList;
                }
                else
                {
                    return null;
                }
            }
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public int getTotalCountByBuyerId(String buyerId)
    {
        try
        {
            String sql = "select count(*) from GoodsCommission gc where gc.buyerId=:buyerId";
            Query query = getSession().createQuery(sql);
            query.setParameter("buyerId", buyerId);
            int num = ((Number) query.iterate().next()).intValue();
            return num;
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return -1;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List getListByGoodsIdAndIndexAndPageSize(String goodsId, int index, int pageSize)
    {
        try
        {
            String sql = "from GoodsCommission gc where gc.goodsId=:goodsId order by gc.buyerId asc,gc.goodsId asc, gc.commissionUpdateTime desc";
            Query query = getSession().createQuery(sql);
            query.setParameter("goodsId", goodsId);
            query.setFirstResult((index - 1) * pageSize);
            query.setMaxResults(pageSize);
            List tempList = query.list();
            if (tempList == null)
            {
                return null;
            }
            else
            {
                if (tempList.size() > 0)
                {
                    return tempList;
                }
                else
                {
                    return null;
                }
            }
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public int getTotalCountByGoodsId(String goodsId)
    {
        try
        {
            String sql = "select count(*) from GoodsCommission gc where gc.goodsId=:goodsId";
            Query query = getSession().createQuery(sql);
            query.setParameter("goodsId", goodsId);
            int num = ((Number) query.iterate().next()).intValue();
            return num;
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return -1;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

}